package com.henghai.zhenxuan.domain.request;

import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

import com.henghai.zhenxuan.data.api.PrizeResAPi;
import com.henghai.zhenxuan.data.bean.HttpBean;
import com.henghai.zhenxuan.data.bean.PrizeResBean;
import com.henghai.zhenxuan.data.repository.DataRepository;
import com.kunminx.architecture.data.response.DataResult;
import com.kunminx.architecture.domain.message.MutableResult;
import com.kunminx.architecture.domain.message.Result;
import com.kunminx.architecture.domain.request.Requester;

import org.jetbrains.annotations.NotNull;

import io.reactivex.disposables.Disposable;

public class PrizeResRequester extends Requester implements DefaultLifecycleObserver {

    private final MutableResult<DataResult<HttpBean<PrizeResBean>>> result = new MutableResult<>();

    public Result<DataResult<HttpBean<PrizeResBean>>> getResult() {
        return result;
    }

    private Disposable mDisposable;

    public void request(LifecycleOwner lifecycleOwner, String prizeId) {
        DataRepository.getInstance().prizeRes(lifecycleOwner, new PrizeResAPi(prizeId), result::postValue);
    }

    public void cancelLogin() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
            mDisposable = null;
        }
    }

    @Override
    public void onStop(@NonNull @NotNull LifecycleOwner owner) {
        cancelLogin();
    }
}
